﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;

using MP.Core.Library;
using MP.Core.Config;
public partial class Admin_Hurman_Controls_AddEditDepart : MP.Core.Authentication.Control.AuthenBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitAuthen(EnumModules.DEPART);

        if (!IsPostBack)
        {
            loadData();
        }
    }

    public Guid Id
    {
        get { return !string.IsNullOrEmpty(Request["Id"]) ? Util.IsGuid(Request["Id"]) ? new Guid(Request["Id"]) : Guid.Empty : Guid.Empty; }
    }

    void loadData()
    {
        MaDepartmentEntity depart = MaDepartmentManager.CreateInstant().GetById(Id);
        if (depart != null)
        {
            if (!IsEdit())
                RedirectLogin();

            txtDepartCode.Text = depart.DepartCode;
            txtDepartName.Text = depart.DepartName;
            txtDescription.Text = depart.Description;

            btnAdd.Visible = false;
            btnEdit.Visible = true;
        }
        else
        {
            if (!IsAdd())
                RedirectLogin();
        }
    }

    MaDepartmentEntity getDepart()
    {
        MaDepartmentEntity depart = new MaDepartmentEntity();
        depart.Id = Guid.NewGuid();
        depart.DepartCode = Filter.GetMaxString(txtDepartCode.Text.Trim(), MaDepartmentFields.DepartCode.MaxLength);
        depart.DepartName = Filter.GetMaxString(txtDepartName.Text.Trim(), MaDepartmentFields.DepartName.MaxLength);
        depart.Description = Filter.GetMaxString(txtDescription.Text.Trim(), MaDepartmentFields.Description.MaxLength);
        depart.UpdatedBy = Util.CurrentUserName;
        depart.UpdatedDate = DateTime.Now;
        return depart;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (!IsAdd())
                RedirectLogin();

            MaDepartmentEntity depart = getDepart();
            depart.CreatedBy = Util.CurrentUserName;
            depart.CreatedDate = DateTime.Now;

            MaDepartmentManager.CreateInstant().Insert(depart);
            Response.Redirect("/Admin/Hurman/ListDepart.aspx");
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (!IsEdit())
                RedirectLogin();

            MaDepartmentEntity depart = getDepart();
            MaDepartmentEntity tmp = MaDepartmentManager.CreateInstant().GetById(Id);
            if (tmp != null)
            {
                depart.Id = tmp.Id;
                depart.CreatedBy = tmp.CreatedBy;
                depart.CreatedDate = tmp.CreatedDate;

                MaDepartmentManager.CreateInstant().Update(depart);
                Response.Redirect("/Admin/Hurman/ListDepart.aspx");
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/Hurman/ListDepart.aspx");
    }

    protected void validDepart_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;

        MaDepartmentEntity depart = MaDepartmentManager.CreateInstant().GetById(Id);
        if (depart != null) // Edit
        {
            if (depart.DepartCode != txtDepartCode.Text.Trim() && MaDepartmentManager.CreateInstant().IsExistsByDepartCode(txtDepartCode.Text.Trim()))
            {
                validDepart.ErrorMessage = "Trùng mã phòng ban";
                args.IsValid = false;
            }
        }
        else // Add
        {
            if (MaDepartmentManager.CreateInstant().IsExistsByDepartCode(txtDepartCode.Text.Trim()))
            {
                validDepart.ErrorMessage = "Trùng mã phòng ban";
                args.IsValid = false;
            }
        }
    }
}
